package monster.com.cvh.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.text.ParseException;
import java.util.List;

import monster.com.cvh.R;
import monster.com.cvh.activity.CommentDetailsActivity;
import monster.com.cvh.activity.NewsDetailsActivity;
import monster.com.cvh.bean.GetCommentInfoBean;
import monster.com.cvh.bean.PraiseBean;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.util.TimeConvert;
import monster.com.cvh.util.TimeUtil;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: monster
 * @data: 2018/4/2
 * @function: 点赞Recyclerview的adapter
 */
public class PraiseAdapter extends BaseQuickAdapter<PraiseBean.DataBean,BaseViewHolder> {
    private String lastTime;
    private boolean is_praise;
    private String pushCommentTime;

    public PraiseAdapter(int layoutResId, @Nullable List<PraiseBean.DataBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final PraiseBean.DataBean item) {
        Glide.with(mContext).load(R.mipmap.mine_official).into((ImageView) helper.getView(R.id.iv_item_praise));
        //获取当前时间戳
        long timeStamp = System.currentTimeMillis() / 1000;
        try {
            String s = TimeConvert.dateToStamp(item.getTime());
            long articleTime = timeStamp - Long.valueOf(s) / 1000;
            lastTime = TimeUtil.calculateTime(articleTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        helper.setText(R.id.tv_item_praise_time,lastTime);
        helper.setText(R.id.tv_item_praise_num,item.getComment_priase_count()+"人赞了你");
        helper.setText(R.id.tv_item_praise_content, SPUtils.getString(mContext,"userName","")+":"+item.getComment_content());
        helper.setText(R.id.tv_item_praise_news_title,"来至:"+item.getArticle_title());
        helper.getView(R.id.tv_item_praise_content).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取单条评论
                if (item.getLv1_comment_id() == null){
                    //comment_id 为 顶层
                    getComment(item.getComment_id());
                }else {
                    double id = Double.parseDouble(String.valueOf(item.getLv1_comment_id()));
                    Log.i(TAG, "onClick: "+id);
                    getComment((int)id);
                }

            }
        });

        //跳转文章
        helper.getView(R.id.tv_item_praise_news_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, NewsDetailsActivity.class);
                intent.putExtra("id", item.getArticle_id());
                intent.putExtra("isCollect", item.isIs_article_collect());
                intent.putExtra("reply",item.getArticle_comment_count());
                mContext.startActivity(intent);
            }
        });
    }

    private void getComment(final int comment_id) {
        OkGo.<String>get(MyConstant.GET_COMMENT_INFO).params("token",SPUtils.getString(mContext,"token","")).params("comment_id",comment_id).execute(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                //获取是否点赞
                Gson gson = new Gson();
                GetCommentInfoBean getCommentInfoBean = gson.fromJson(s, GetCommentInfoBean.class);
                switch (getCommentInfoBean.getCode()){
                    case 1:
                        is_praise = getCommentInfoBean.getData().isIs_praise();
                        //获取当前时间戳
                        long timeStamp = System.currentTimeMillis() / 1000;
                        try {
                            String ss = TimeConvert.dateToStamp(getCommentInfoBean.getData().getPublish_time());
                            long articleTime = timeStamp - Long.valueOf(ss) / 1000;
                            pushCommentTime = TimeUtil.calculateTime(articleTime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        //后期优化（传对象）
                        Intent intent = new Intent(mContext, CommentDetailsActivity.class);
                        intent.putExtra("isPraise", is_praise);
                        intent.putExtra("identify",2);
                        intent.putExtra("id",comment_id);
                        intent.putExtra("time",pushCommentTime);
                        intent.putExtra("userId",getCommentInfoBean.getData().getUser_id());
                        intent.putExtra("userName",getCommentInfoBean.getData().getUser_name());
                        intent.putExtra("content",getCommentInfoBean.getData().getContent());
                        intent.putExtra("reply",getCommentInfoBean.getData().getReply());
                        intent.putExtra("iconUrl",getCommentInfoBean.getData().getIcon_url());
                        intent.putExtra("praise",getCommentInfoBean.getData().getPraise_count());
                        mContext.startActivity(intent);
                        break;
                }
            }
        });
    }
}
